<?php
namespace app\common\service;

use app\common\model\TagModel;
use app\common\model\TagRelModel;

class TagService{


    const OBJ_TYPE_LUNWEN = 2;
    const OBJ_TYPE_VGMS_USER = 1;
    const TAG_TYPE_SCHOOL = 1;
    const TAG_TYPE_TEACHER = 2;
    const TAG_TYPE_COUSTOM = 3;
    const TAG_TYPE_STUDENT_POINT = 4;

    public static function addTag($tag_name, $uid = 0, $tag_type, $obj_id, $obj_type){
        $current_tag = null;
        $tag_map = [];
        $tag_map['tag_name'] = $tag_name;
        if($uid){
            $tag_map['tag_uid'] = $uid;
        }
        $current_tag = TagModel::where($tag_map)->find();
        $tag_id = 0;
        if(!$current_tag){
            $tag = new TagModel();
            $tag->tag_name = $tag_name;
            $tag->tag_uid = $uid;
            $tag->tag_type = $tag_type;
            $tag->save();
            $tag_id = $tag->id;
        }else{  
            $tag_id = $current_tag->id;
        }
        $tagRel = TagRelModel::where(['obj_id' => $obj_id, 'obj_type' => $obj_type, 'tag_id' => $tag_id])->find();
        if(!$tagRel){
            $tagRel = new TagRelModel();
            $tagRel->obj_id = $obj_id;
            $tagRel->obj_type = $obj_type;
            $tagRel->tag_id = $tag_id;
            $tagRel->save();
        }
        return $tagRel;
    }

}